package com.example.demo.practice.enterprise;


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.monitoring.AlarmList;
import com.example.demo.practice.tool.ServerHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FireTrain extends IBaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_train);
        setTitle("消防培训");
        serverHttp=new ServerHttp(new UpdateUI());
        serverHttp.setUrl("getAllTrain");
    }

    class UpdateUI extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            JSONArray jsonArray=serverHttp.getData();
            Log.e("sss", String.valueOf(jsonArray));
            serverHttp.clear();
            List<Map<String, Object>> groupArray=new ArrayList<Map<String, Object>> ();
            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    map.put("name", json.getString("name"));
                    map.put("company", json.getString("company"));
                    map.put("address", json.getString("address"));
                    map.put("theme", json.getString("theme"));
                    map.put("time", json.getString("time"));
                    groupArray.add(map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jsonArray = null;
            SimpleAdapter simpleAdapter = new SimpleAdapter(FireTrain.this,
                    groupArray,
                    R.layout.list_train_item,
                    new String[]{"name", "company", "address", "theme", "time"},
                    new int[]{R.id.name, R.id.company, R.id.address,R.id.theme, R.id.time});
            ListView listView = (ListView) findViewById(R.id.list);
            listView.setAdapter(simpleAdapter);
            listView.setDivider(new ColorDrawable(getResources().getColor(R.color.LineView)));
            listView.setDividerHeight(1);
        }
    }
}
